package com.transcargo.users;

import java.util.ArrayList;
import java.util.Iterator;

import com.transcargo.data.Cache;
import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.geo.Country;
import com.transcargo.geo.GeoObjectNotFoundException;
import com.transcargo.rating.Rating;


public class LongUser {
	private int Id = -1;
	private Contact[] contacts = null;
    private Transport[] transports = null;
    private int num_contacts = 0;
    private int num_transports = -1;
    private String name = "";
    private String address = "";
    private String city = "";
    private String country = null;
    private String phone = "";
    private String email = "";
    private String url = "";
    private String skype = "";
    private String icq = "";
    private String info = "";
    private String timezone = "";
    private String registered = "";
    private int num_cargo = 0;
    private int num_freight = 0;
    private Rating rating = null;
    
    public LongUser(int id, db dbase) throws UserNotFoundException, GeoObjectNotFoundException {
		Id = id;
        try {
            ArrayList<String[]> list = dbase.execute("SELECT organization, address, city, country_id, phone, email, web_url, skype, icq, timezone, info, registered FROM registered_users WHERE id=" + Integer.toString(id));
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter.hasNext()) {
                    String[] ctr = iter.next();
                    name = ctr[0];
                    address = ctr[1];
                    city = ctr[2];
                    phone = ctr[4];
                    email = ctr[5];
                    url = ctr[6];
                    skype = ctr[7];
                    icq = ctr[8];
                    info = ctr[10];
                    timezone = ctr[9];
                    registered = ctr[11];
                    country = Cache.getCountry(Integer.parseInt(ctr[3])).getName();
                    
                	list = dbase.execute("SELECT count(id) FROM posted_cargo WHERE visible=1 AND (user_id mod 100000000)=" + Id);
                	iter = list.iterator();
                	if (iter.hasNext()) {
                		ctr = iter.next();
                		num_cargo = Integer.parseInt(ctr[0]);
                	}
                	list = dbase.execute("SELECT count(id) FROM posted_transport WHERE visible=1 AND (user_id mod 100000000)=" + Id);
                	iter = list.iterator();
                	if (iter.hasNext()) {
                		ctr = iter.next();
                		num_freight = Integer.parseInt(ctr[0]);
                	}
                    list = dbase.execute("SELECT id FROM transport WHERE user_id=" + Integer.toString(id));
                    if (list != null) {
                        iter =list.iterator();
                        num_transports = list.size();
                    }
                    list = dbase.execute("SELECT id FROM contacts WHERE user_id=" + Integer.toString(id));
                    if (list != null) {
                        iter =list.iterator();
                        num_contacts = list.size();
                        contacts = new Contact[num_contacts];
                        int i = 0;
                        while (iter.hasNext()) {
                            ctr = iter.next();
                            int contact_id = Integer.parseInt(ctr[0]);
                            contacts[i] = new Contact(contact_id, id, dbase);
                            i++;
                        }
                    }
                    rating = new Rating(Id, dbase);
                } else throw new UserNotFoundException(id);
            } else throw new UserNotFoundException(id);
        } catch (dbException e) {
            throw new UserNotFoundException(id);
        }
	}
	public int getNumContacts() {
		return num_contacts;
	}
	public Contact[] getContacts() {
		return contacts;
	}
	public Contact getContact(int id) {
		for (int i = 0; i < contacts.length; i++) if (contacts[i].getId() == id)
		return contacts[i];
		return null;
	}
	public String getName() {
		return name;
	}
    public String getAddress() {
        return address;
    }
    public String getCity() {
        return city;
    }
    public String getPhone() {
        return phone;
    }
    public String getEmail() {
        return email;
    }
    public String getUrl() {
        return url;
    }
    public String getSkype() {
        return skype;
    }
    public String getIcq() {
        return icq;
    }
    public String getInfo() {
        return info;
    }
    public String getTimezone() {
        return timezone;
    }
    public String getCountry() {
        return country;
    }
    public String getRegisterDate() {
        return registered;
    }
    public int getNumTransports() {
        return num_transports;
    }
    public Transport[] getTransport(db dbase) throws dbException, TransportNotFoundException {
        if (transports == null && num_transports > 0) {
            ArrayList<String[]> list = dbase.execute("SELECT id FROM transport WHERE user_id=" + Integer.toString(Id));
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter == null || !iter.hasNext()) {
                    num_transports = 0;
                    return null;
                } else {
                    num_transports = list.size();
                    transports = new Transport[num_transports];
                    int i = 0;
                    while (iter.hasNext()) {
                        String[] ctr = iter.next();
                        int tid = Integer.parseInt(ctr[0]);
                        transports[i] = new Transport(tid, Id, dbase);
                        i++;
                    }
                    num_transports = i;
                }
            }
        }
        return transports;
    }
    public Transport getTransport(int id, db dbase) throws dbException, TransportNotFoundException {
    	if (transports == null && num_transports > 0) getTransport(dbase);
        for (int i = 0; i < num_transports; i++) if (transports[i].getId() == id) return transports[i];
        return null;
    }
    public int getNumCargo() {
    	return num_cargo;
    }
    public int getNumFreight() {
    	return num_freight;
    }
    public Rating getRating() {
    	return rating;
    }
    public static Boolean validateUser(int id, db dbase) {
        try {
            ArrayList<String[]> list = dbase.execute("SELECT id FROM registered_users WHERE id=" + Integer.toString(id));
            if (list != null) {
                Iterator<String[]> iter =list.iterator();
                if (iter.hasNext()) {
                    return true;
                } else return false;
            } else return false;
        } catch (dbException e) {
           return false;
        }
    }
}
